using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace AutoBurnerLibrary
{

   
    /// <summary>
    /// List of files that are waiting to be burned to CD
    /// </summary>
    public sealed class BurnList
    {
        static BurnList instance = null;
        static readonly object padlock = new object();
        private List<BurnItem> burnItems = null;
        private long ListTotalMemorySize = 0;//Dividing the total memory size by 1048576 will give you number in mb
        private long ListLimitSize = 681574400;

        BurnList()
        {
            this.burnItems = new List<BurnItem>();
        }

        public static BurnList Instance
        {
            get
            {
                lock (padlock)
                {
                    if (instance == null)
                    {
                        instance = new BurnList();
                    }
                    return instance;
                }
            }
        }

        /// <summary>
        /// Adds one item to the burnList
        /// </summary>
        /// <param name="burnItem">File item that will be burned</param>
        /// <returns>BurnList list of items to burn</returns>
        public static BurnList AddItemToBurnList(BurnItem burnItem)
        {
            //Get the running instance of burnlist
            BurnList burnList = BurnList.Instance;
            
            //Make sure the new burnItem isn't already in the list            
            if(!burnList.burnItems.Contains(burnItem)){
                burnList.burnItems.Add(burnItem);
                burnList.ListTotalMemorySize += burnItem.FileSize;

                if (burnList.ListTotalMemorySize >= burnList.ListLimitSize)
                {
                    BurnerWrapper.BurnFiles(burnList.burnItems);
                }

            }

            return burnList;

        }

        /// <summary>
        /// Remove an item from the burnList
        /// </summary>
        /// <param name="burnItem">Item to be removed</param>
        public static void RemoveItemFromList(BurnItem burnItem)
        {
            BurnList burnList = BurnList.Instance;
            int index = burnList.burnItems.IndexOf(burnItem);

            if (index > 0) { burnList.burnItems.RemoveAt(index); }

        }

        /// <summary>
        /// Replace item on a list. This function is used when a file has been renamed
        /// </summary>
        /// <param name="oldItem"></param>
        /// <param name="newItem"></param>
        public static void ReplaceItemOnBurnList(BurnItem oldItem, BurnItem newItem)
        {
            BurnList.RemoveItemFromList(oldItem);
            BurnList.AddItemToBurnList(newItem);
        }

        /// <summary>
        /// Lets us know how long before we need to start burning a new CD
        /// </summary>
        /// <param name="limit">Limit before we should burn
        /// </param>
        public void SetLimit(long limit)
        {            
            if (limit < 1) { throw new ArgumentException("List Limit size can not be less than 1 byte."); }

            this.ListLimitSize = limit;
        }
    }


}

